package org.delphy.indy2.controls
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.controls.Button;
	
	import org.delphy.indy2.controls.carbonTabSelectorClasses.*;

	public class CarbonTabSelector extends Canvas
	{
		
		private var track:CarbonTabSelectorTrack;
		
		private var childStartY:Number=30;
		private var childStartX:Number;
		
		public function CarbonTabSelector()
		{
			super();
			
			styleName="carbonTabSelector";
			
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
			track=new CarbonTabSelectorTrack();
			rawChildren.addChild(track);
			
			childStartX=getStyle("trackWidth")/2;
			
			for(var i:uint=0;i<numChildren;i++){
				var child:Button=getChildAt(i) as Button;
				
				child.x=childStartX;
				child.y=height/(numChildren+1)*i+30;
				
				child.addEventListener(MouseEvent.CLICK,onTabClick);
				child.addEventListener(MouseEvent.MOUSE_DOWN,onTabMouseDown);
				child.addEventListener(MouseEvent.MOUSE_UP,onTabMouseUp);
			}
		}
		
		private function onTabMouseDown(evt:MouseEvent):void{
			var child:Button=evt.currentTarget as Button;
			child.startDrag(false,new Rectangle(childStartX,childStartY,childStartX,height-child.height));
		}

		private function onTabMouseUp(evt:MouseEvent):void{
			evt.currentTarget.stopDrag();
		}
		
		private function onTabClick(evt:MouseEvent):void{
			for(var i:uint=0;i<numChildren;i++){
				var child:Button=getChildAt(i) as Button;
				child.selected=false;
			}
			
			evt.currentTarget.selected=true;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			track.x=0;
			track.y=10;
			track.width=getStyle("trackWidth");
			track.height=unscaledHeight;
		}
		
	}
}